<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="数据模型名称" prop="modelName">
        <el-input
          v-model="queryParams.dataModelName"
          placeholder="请输入数据模型名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dept:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="dataModelList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="id" label="数据模型编号" width="300"></el-table-column>
      <el-table-column prop="name" label="数据模型名称" width="400"></el-table-column>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dept:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dept:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body >
        <el-form ref="form" :model="dataModel" :rules="rules" label-width="190px">
            <el-row>
            <el-form-item label="模型名称" prop="modelName">
              <el-input v-model="dataModel.modelName" placeholder="请输入模型名称" />
            </el-form-item>
            </el-row>
            <el-table :data="dataModel.features" border stripe style="width: 100%;">
              <el-table-column prop="key" label="特征Key值(Feature Key)">
                <template slot-scope="scope">
                  <el-input v-show="scope.row.edit" v-model="scope.row.key" placeholder="特征值"></el-input>
                  <span v-show="!scope.row.edit">{{scope.row.key}}</span>
                </template>
              </el-table-column>
              <el-table-column prop="key" label="特征描述">
                <template slot-scope="scope">
                  <el-input v-if="scope.row.edit" v-model="scope.row.description" placeholder="特征描述"></el-input>
                  <span v-else>{{scope.row.description}}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" v-if="!readonly">
                <template slot-scope="scope">
                  <el-button v-if="scope.row.edit" type="text" size="medium" @click="confirmAdd(scope.row)">
                    <i class="el-icon-check" aria-hidden="true"></i>
                  </el-button>
                  <div v-else>
                    <el-button type="text" size="medium" @click="editDataModelRow(scope.row)">
                      <i class="el-icon-edit" aria-hidden="true"></i>
                    </el-button>
                    <el-button type="text" size="medium" @click="deleteDataModelRow(scope.row,scope.$index)">
                      <i class="el-icon-delete" aria-hidden="true"></i>
                    </el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
            <el-button type="text" @click="addDataModelRow" v-if="!readonly">添加特征</el-button>
            <el-divider></el-divider>
            <el-button @click="submitForm" v-if="!readonly">提交 </el-button>
            <el-button @click="cancel" v-else>关闭 </el-button>
        </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addDataModel, updateDataModel,deleteDataModel,getDataModelById,getDataModels } from '@/api/manage/dataModel'
// import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "DataModel",
  dicts: ['sys_normal_disable'],
  // components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      dataModelList: [],
      // // 部门树选项
      // coopOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      active:0,
      editForm: {},
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,

      // 查询参数
      queryParams: {
        modelName: undefined,
        // status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // parentId: [
        //   { required: true, message: "上级银行不能为空", trigger: "blur" }
        // ],
        modelName: [
          { required: true, message: "，模型名称" +
              "名称不能为空", trigger: "blur" }
        ],


      },
      dataModel:{
      features:[]
      },
      reviseDaraModel:{

      },
      readonly: false
    };
  },

  created() {
    this.getList();
  },
  computed:{

  },
  methods: {
    addDataModelRow(){
      // const that = this;
      this.dataModel.features.push({
        edit:true,
      });
    },
    deleteDataModelRow(row,index){
      if(index >= 0){
        this.dataModel.features.splice(index,1);
      }
    },
    editDataModelRow(row){
      // console.log(row);
      row.edit = true;
    },
    confirmAdd(row) {
      row.edit = false;
    },
    openDig() {
      this.active = 0; //重置active
      this.open = true; //设置open为true显示dialog对话框
    },
    prve() {
      --this.active;//active自减一
      if (this.active < 0) this.active = 0;//设置active最小值
    },
    next() {
      ++this.active;
      if (this.active > 2) this.active = 2;
    },


    /** 查询数据模型列表 */
    getList() {
      this.loading = true;
      getDataModels().then(response => {
        this.dataModelList = response.data;
        // console.log(this.dataModelList);
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.dataModel = {
        features:[]
        // dataModelId: undefined,
        // modelName: undefined,
        // description: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      // console.log(row)
      this.reset();
      this.readonly = false;
      this.open = true;
      this.title = "添加数据模型";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    handleView(row) {
      this.reset();
      getDataModelById({modelId:row.id}).then(response => {
        this.dataModel = response.data;
        let tempFeature = [];
        for(let item in this.dataModel.features){
          let feature = this.dataModel.features[item];
          tempFeature.push({"key":feature.key,
            "description":feature.description,
            "modelId":feature.modelId,
            "featureId":feature.featureId,
            "edit":false
          })
        }
        this.dataModel.features = tempFeature;
        console.log(this.dataModel);
        this.readonly = true;
        this.open = true;
        this.title = "修改数据模型";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getDataModelById({modelId:row.id}).then(response => {
        this.dataModel = response.data;
        let tempFeature = [];
        for(let item in this.dataModel.features){
          let feature = this.dataModel.features[item];
          tempFeature.push({"key":feature.key,
            "description":feature.description,
            "modelId":feature.modelId,
            "featureId":feature.featureId,
            "edit":false
          })
          // this.dataModel.features[item]["edit"]=true;
        }
        this.dataModel.features = tempFeature;
        // this.dataModel.modelId = response.data[0].modelId;
        console.log(this.dataModel);
        this.readonly = false;
        this.open = true;
        this.title = "修改数据模型";
      });
      // listDataObject().then(response => {
      //   this.coopOptions = response.data
      // });
    },
    /** 提交按钮 */
    submitForm: function() {
      // console.log(this.dataModel.modelId+"========");
      this.$refs["form"].validate(valid => {

        if (valid) {

          if (this.dataModel.modelId != undefined) {
            // console.log(this.dataModel.modelId+"========");
            updateDataModel(this.dataModel).then(response =>{

              // console.log(response.message+"1111111");
              if (response.message == "操作成功") {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.$modal.msgSuccess(response.message);
              }
            })
          }else {
            addDataModel(this.dataModel).then(response => {
              // console.log(this.dataModel);
              if (response.message == "操作成功") {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.$modal.msgSuccess(response.message);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项？').then(function() {
        return deleteDataModel(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
